#include "KtLoginSettingWgt.h"

#include <QFile>

KtLoginSettingWgt::KtLoginSettingWgt(QWidget *parent)
	: QWidget(parent)
	, ui(new Ui::KtLoginSettingWgtClass())
{
	ui->setupUi(this);
	setLoginSettingInfo(m_info);
	initStyle();
	initConnect();
}

KtLoginSettingWgt::~KtLoginSettingWgt()
{
	delete ui;
}

void KtLoginSettingWgt::setLoginSettingInfo(const KtLoginSettingInfo& info)
{
	ui->serverIPLEdit->setText(info.ip.trimmed());
	ui->serverPortLEdit->setText(info.port.trimmed());
	ui->sslChBox->setChecked(info.ssl);
	m_info = info;
}

KtLoginSettingInfo KtLoginSettingWgt::getLoginSettingInfo()
{
	KtLoginSettingInfo info;
	info.ip = ui->serverIPLEdit->text().trimmed();
	info.port = ui->serverPortLEdit->text().trimmed();
	info.ssl = ui->sslChBox->isChecked();
	return info;
}

void KtLoginSettingWgt::sureBtnClickedSlt()
{
	m_info = getLoginSettingInfo();
	Q_EMIT sureBtnClickedSgl(m_info);
}

void KtLoginSettingWgt::cancelBtnClickedSlt()
{
	setLoginSettingInfo(m_info);
	Q_EMIT cancelBtnClickedSgl();
}

void KtLoginSettingWgt::initConnect()
{
	connect(ui->sureBtn, &QPushButton::clicked, this, &KtLoginSettingWgt::sureBtnClickedSlt);
	connect(ui->cancelBtn, &QPushButton::clicked, this, &KtLoginSettingWgt::cancelBtnClickedSlt);
}

void KtLoginSettingWgt::initStyle()
{
	ui->serverIPLEdit->setContextMenuPolicy(Qt::NoContextMenu);
	ui->serverIPLab->setText(tr("IP Address"));
	ui->serverPortLEdit->setContextMenuPolicy(Qt::NoContextMenu);
	ui->serverPortLab->setText(tr("Port"));
	ui->sureBtn->setCursor(Qt::PointingHandCursor);
	ui->sureBtn->setText(tr("Sure"));
	ui->cancelBtn->setCursor(Qt::PointingHandCursor);
	ui->cancelBtn->setText(tr("Cancel"));
	ui->sslChBox->setObjectName("sslChBox");
	ui->sslChBox->setText(tr("SSL Encryption"));
	ui->sslChBox->setCursor(Qt::PointingHandCursor);
	QFile file(":/ktloginwidget/resource/qss/ktloginsettingwgt.qss");
	file.open(QFile::ReadOnly);
	setStyleSheet(QLatin1String(file.readAll()));
}
